package com.aaxis.microservice.training.demo1.controller;

import com.aaxis.microservice.training.demo1.domain.User;
import com.aaxis.microservice.training.demo1.message.KafkaProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by terrence on 2018/10/22.
 */
@RestController
@RequestMapping(value = "/rest/message", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class RestMessageController {

    private static final String ENV_KAFKA_TEMPLATE_DEFAULT_TOPIC = "spring.kafka.template.default-topic";

    @Autowired
    private Environment mEnvironment;

    @Autowired
    private KafkaProducer mKafkaProducer;



    @GetMapping("/send/{data}")
    public void send(@PathVariable String data) {
        mKafkaProducer.sendMessage(mEnvironment.getProperty(ENV_KAFKA_TEMPLATE_DEFAULT_TOPIC), data);
    }



    @GetMapping("/sendObj/user")
    public void sendObjUser(User pUser) {
        log.info("sendObjUser() user id:{}, name:{}, pass:{}", pUser.getId(), pUser.getUsername(), pUser.getUsername());
        mKafkaProducer.sendMessage(mEnvironment.getProperty(ENV_KAFKA_TEMPLATE_DEFAULT_TOPIC), pUser);
    }
}